package com.usefullc.english.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.common.enums.ValidActionEnum;
import com.usefullc.english.build.VideoBuilder;
import com.usefullc.english.param.ContentBuildParam;
import com.usefullc.english.param.ContentStatusParam;
import com.usefullc.system.validator.UsefulValid;
import com.usefullc.english.domain.Content;
import com.usefullc.english.param.ContentParam;
import com.usefullc.english.vo.ContentVo;
import com.usefullc.english.query.ContentQuery;
import com.usefullc.english.service.ContentService;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdsParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import javax.validation.Valid;

import static com.usefullc.system.controller.WebResultBuilder.successResult;

/**
 * 内容控制类
 * @author: 星空
 * @date: 2022-08-07
 */
@RestController
@RequestMapping("/english/content/")
@Api( tags="内容")
public class ContentController extends BaseController {

    @Autowired
    private ContentService contentService;

    @Autowired
    private VideoBuilder videoBuilder;

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "内容列表",response = WebResult.class)
    @GetMapping(path = "list",produces = "application/json")
    public WebResult list(@UsefulValid(query = true) ContentQuery query) {
        Pager<ContentVo> page = contentService.findVoByPage(query);
        return WebResultBuilder.successResult(page);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "内容所有列表", response = WebResult.class)
    @GetMapping(path = "listAll", produces = "application/json")
    public WebResult listAll(@UsefulValid(query = true) ContentQuery query) {
        List<Content> dataList = contentService.findAll(query);
        return WebResultBuilder.successResult(dataList);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "内容详情",response = WebResult.class)
    @GetMapping(path = "detail",produces = "application/json")
    public WebResult detail(@ApiParam(value = "示例ID") @Valid @RequestParam Long id) {
        Content content = contentService.findById(id);
        return WebResultBuilder.successResult(content);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "创建内容", response = WebResult.class)
    @PostMapping(path = "/save", produces = "application/json")
    @UsefulValid
    public WebResult save(@UsefulValid(save=true) @RequestBody ContentParam param) {
        param.setId(null);
        contentService.saveContent(param);
        return successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "修改内容", response = WebResult.class)
    @PostMapping(path = "/update", produces = "application/json")
    public WebResult update(@UsefulValid(update=true) @RequestBody ContentParam param) {
        contentService.saveContent(param);
        return successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "del",produces = "application/json")
    @ApiOperation(value = "删除内容",response = WebResult.class)
    public WebResult del(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        contentService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "delByLogic",produces = "application/json")
    @ApiOperation(value = "逻辑删除内容",response = WebResult.class)
    public WebResult delByLogic(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        contentService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }


    @ApiOperation(value = "设置开启回复", response = WebResult.class)
    @PostMapping(path = "setReply", produces = "application/json")
    public WebResult setReply(@UsefulValid @RequestBody ContentStatusParam param) {
        contentService.getBaseMapper().setReply(param.getIds(), param.getStatus());
        return WebResultBuilder.successResult();
    }

    @ApiOperation(value = "设置推荐", response = WebResult.class)
    @PostMapping(path = "setRecommend", produces = "application/json")
    public WebResult setRecommend(@UsefulValid @RequestBody ContentStatusParam param) {
        contentService.getBaseMapper().setRecommend(param.getIds(), param.getStatus());
        return WebResultBuilder.successResult();
    }

    @ApiOperation(value = "设置置顶", response = WebResult.class)
    @PostMapping(path = "setTop", produces = "application/json")
    public WebResult setTop(@UsefulValid @RequestBody ContentStatusParam param) {
        contentService.getBaseMapper().setTop(param.getIds(), param.getStatus());
        return WebResultBuilder.successResult();
    }

    @ApiOperation(value = "修改投稿状态", response = WebResult.class)
    @PostMapping(path = "updateContributionStatus", produces = "application/json")
    public WebResult updateContributionStatus(@UsefulValid @RequestBody ContentStatusParam param) {
        contentService.getBaseMapper().updateContributionStatus(param.getIds(), param.getStatus());
        return WebResultBuilder.successResult();
    }

    @ApiOperation(value = "内容状态修改", response = WebResult.class)
    @PostMapping(path = "updateStatus", produces = "application/json")
    public WebResult updateStatus(@UsefulValid @RequestBody ContentStatusParam param) {
        contentService.getBaseMapper().updateStatus(param.getIds(), param.getStatus());
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "构建内容", response = WebResult.class)
    @PostMapping(path = "/build", produces = "application/json")
    public WebResult build(@UsefulValid @RequestBody ContentBuildParam param) {
//        videoBuilder.build(param);
//        videoBuilder.buildNew(param);
        videoBuilder.buildFull();
        return successResult();
    }


}
